#!/usr/bin/env bash
CWD=$(cd `dirname $0`; pwd)
BASE_DIR=$(dirname ${CWD})

cd ${CWD}
sed "s@\${BASE_DIR}@${BASE_DIR}@g" template.daphne.service > avl-ztp-daphne.service
sed "s@\${BASE_DIR}@${BASE_DIR}@g" template.uwsgi.service > avl-ztp-api.service
sed "s@\${BASE_DIR}@${BASE_DIR}@g" template.sua.service > avl-ztp-sua.service
sed "s@%(BASE_DIR)@${BASE_DIR}@g"  template.uwsgi.ini > uwsgi_dev.ini
mv avl-ztp-daphne.service avl-ztp-api.service avl-ztp-sua.service -t /usr/lib/systemd/system/
systemctl enable avl-ztp-daphne
systemctl start zavl-ztp-daphne
systemctl enable avl-ztp-api
systemctl start avl-ztp-api
systemctl enable avl-ztp-sua
systemctl start avl-ztp-sua
echo -e "*/10 * * * * root cd ${BASE_DIR} && ${BASE_DIR}/env/bin/python manage.py collect_device_info > ${BASE_DIR}/cron_error.log &\n" > /etc/cron.d/ztp_collect_device_info
echo -e "00 04 * * * root cd ${BASE_DIR} && ${BASE_DIR}/env/bin/python manage.py anomaly_train > ${BASE_DIR}/cron_error.log &\n" > /etc/cron.d/ztp_anomaly_train
echo -e "30 01 * * * root cd ${BASE_DIR} && ${BASE_DIR}/env/bin/python manage.py update_user_score > ${BASE_DIR}/cron_error.log &\n" > /etc/cron.d/update_user_score
systemctl status avl-ztp-daphne
systemctl status avl-ztp-sua
systemctl status avl-ztp-api